package com.java.basis.thread.n3.jiaoti;

import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import sun.plugin2.applet.Applet2Status;

import javax.swing.*;
import java.util.concurrent.locks.LockSupport;

/**
 * @author wangning
 * @create 2022-01-25 11:23
 */
@Slf4j(topic = "c.Test03")
public class Test03 {
	static Thread t1;
	static Thread t2;
	static Thread t3;
	public static void main(String[] args) throws InterruptedException {
		ParkUnpark parkUnpark = new ParkUnpark(5);
		t1 = new Thread(() -> {
			parkUnpark.print("a", t2);
		},"t1");
		t2 = new Thread(() -> {
			parkUnpark.print("b", t3);
		},"t2");
		t3 = new Thread(() -> {
			parkUnpark.print("c", t1);
		}, "t3");
		t1.start();
		t2.start();
		t3.start();
		LockSupport.unpark(t1);
	}
}

class ParkUnpark {
	private int loopNumber;

	public ParkUnpark(int loopNumber) {
		this.loopNumber = loopNumber;
	}

	public void print(String str, Thread next) {
		for (int i = 0; i < loopNumber; i++) {
			LockSupport.park();
			System.out.print(str);
			LockSupport.unpark(next);

		}
	}
}
